package com.anyou.mall.goo.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.utils.DateUtils;
import com.anyou.common.utils.TimeUtil;
import com.anyou.mall.goo.domain.GoodsCouponHistory;
import com.anyou.mall.goo.domain.GoodsCouponRelation;
import com.anyou.mall.goo.goods.domain.GooGoods;
import com.anyou.mall.goo.goods.mapper.GooGoodsMapper;
import com.anyou.mall.goo.mapper.GoodsCouponHistoryMapper;
import com.anyou.mall.goo.mapper.GoodsCouponRelationMapper;
import com.anyou.mall.goo.param.SendGoodsCouponParam;
import com.anyou.mall.goo.service.IGooCouponShopService;
import com.anyou.mall.goo.vo.SysUserVo2;
import com.anyou.mall.sho.mapper.CarPartsShopAuditMapper;
import com.anyou.mall.sho.mapper.CarPartsShopMapper;
import com.anyou.mall.use.domain.User;
import com.anyou.mall.use.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.goo.mapper.GoodsCouponMapper;
import com.anyou.mall.goo.domain.GoodsCoupon;
import com.anyou.mall.goo.service.IGoodsCouponService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品优惠券Service业务层处理
 *
 * @author gaochaohao
 * @date 2022-03-28
 */
@Service
public class GoodsCouponServiceImpl extends ServiceImpl<GoodsCouponMapper,GoodsCoupon> implements IGoodsCouponService
{
    @Autowired
    private GoodsCouponMapper goodsCouponMapper;

    @Autowired
    private CarPartsShopMapper carPartsShopMapper;

    @Autowired
    private GoodsCouponRelationMapper goodsCouponRelationMapper;

    @Autowired
    private GoodsCouponHistoryMapper goodsCouponHistoryMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GooGoodsMapper gooGoodsMapper;

    @Autowired
    private CarPartsShopAuditMapper carPartsShopAuditMapper;

    @Autowired
    private IGooCouponShopService gooCouponShopService;

    /**
     * 查询商品优惠券
     *
     * @param goodsCouponId 商品优惠券主键
     * @return 商品优惠券
     */
    @Override
    public GoodsCoupon selectGoodsCouponByGoodsCouponId(Long goodsCouponId)
    {
        GoodsCoupon goodsCoupon = goodsCouponMapper.selectGoodsCouponByGoodsCouponId(goodsCouponId);
/*        List<GoodsCouponHistory> goodsCouponHistories = goodsCouponHistoryMapper.selectGoodsCouponHistoryListByGoodsCouponId(goodsCouponId);
        if (goodsCouponHistories != null) {
            goodsCoupon.setUseHistory(goodsCouponHistories);
        }*/
        List<GoodsCouponHistory> goodsCouponHistories = goodsCouponHistoryMapper.selectList(new LambdaQueryWrapper<GoodsCouponHistory>()
                .eq(GoodsCouponHistory::getGoodsCouponId,goodsCouponId)
                .eq(GoodsCouponHistory::getUseStatus, 1)
                .eq(GoodsCouponHistory::getDelFlag, 0));
        goodsCouponHistories.forEach(e->{
            e.setUser(userMapper.selectById(e.getUserId()));
        });
        if (!goodsCouponHistories.isEmpty()){
            goodsCoupon.setUseHistory(goodsCouponHistories);
        }
        return goodsCoupon;
    }

    @Override
    public List<GoodsCouponHistory> useRecord(Long goodsCouponId) {
        GoodsCoupon goodsCoupon = goodsCouponMapper.selectGoodsCouponByGoodsCouponId(goodsCouponId);
        List<GoodsCouponHistory> goodsCouponHistories = goodsCouponHistoryMapper.selectList(new LambdaQueryWrapper<GoodsCouponHistory>()
                .eq(GoodsCouponHistory::getGoodsCouponId,goodsCouponId)
                .eq(GoodsCouponHistory::getUseStatus, 1)
                .eq(GoodsCouponHistory::getDelFlag, 0));
        goodsCouponHistories.forEach(e->{
            e.setUser(userMapper.selectById(e.getUserId()));
        });
        return goodsCouponHistories;
    }

    /**
     * 查询商品优惠券列表
     *
     * @param goodsCoupon 商品优惠券
     * @return 商品优惠券
     */
    @Override
    public List<GoodsCoupon> selectGoodsCouponList(GoodsCoupon goodsCoupon)
    {
        //获取优惠券列表
        List<GoodsCoupon> goodsCouponList = goodsCouponMapper.selectGoodsCouponList(goodsCoupon);
        return goodsCouponList;
    }

    /**
     * 新增商品优惠券
     *
     * @param goodsCouponParam 商品优惠券
     * @return 结果
     */
    @Override
    @Transactional
    public int insertGoodsCoupon(GoodsCoupon goodsCouponParam)
    {
        //if(StringUtils.isEmpty(goodsCouponParam.getAvailableStore())){
        //    throw new GlobalException("可用商家不能为空");
        //}
        GoodsCoupon goodsCoupon = new GoodsCoupon();
        BeanUtils.copyProperties(goodsCouponParam,goodsCoupon);
        goodsCoupon.setRemainCount(goodsCouponParam.getCouponCount());
        goodsCoupon.setDelFlag("0");
        goodsCouponMapper.insert(goodsCoupon);

        //保存优惠券与汽配商关联表
        //String[] carPartsShopIds = goodsCouponParam.getAvailableStore().split(",");
        //Arrays.asList(carPartsShopIds).forEach(carPartsShopId->{
        //    GooCouponShop gooCouponShop = new GooCouponShop();
        //    gooCouponShop.setCarPartsShopId(Long.parseLong(carPartsShopId));
        //    gooCouponShop.setGoodsCouponId(goodsCoupon.getGoodsCouponId());
        //    gooCouponShopService.save( gooCouponShop);
        //});

        //保存单品卷,类品卷数据
//        goodsCoupon.getGoodsCouponRelation().forEach(e->{
//            // 单品卷
//            if (e.getGoodsId()!=null&&e.getGoodsId()!=0){
//                //先查询是否存在
//                GoodsCouponRelation one = goodsCouponRelationMapper.selectOne(new LambdaQueryWrapper<GoodsCouponRelation>()
//                        .eq(GoodsCouponRelation::getGoodsCouponId, goodsCoupon.getGoodsCouponId())
//                        .eq(GoodsCouponRelation::getGoodsId, e.getGoodsId()));
//                if(one==null){
//                    GoodsCouponRelation CouponRelation = new GoodsCouponRelation();
//                    CouponRelation.setGoodsId(e.getGoodsId());
//                    GooGoods gooGoods = gooGoodsMapper.selectById(e.getGoodsId());
////                CouponRelation.setCarPartsShopId(gooGoods.getCarPartsShopId());
//                    CouponRelation.setGoodsCouponId(goodsCoupon.getGoodsCouponId());
//                    goodsCouponRelationMapper.insert(CouponRelation);
//                }
//
//            }
//            // 类品卷
//            if (e.getPartsClassifyId()!=null&&e.getPartsClassifyId()!=0){
//                //先查询是否存在
//                GoodsCouponRelation one = goodsCouponRelationMapper.selectOne(new LambdaQueryWrapper<GoodsCouponRelation>()
//                        .eq(GoodsCouponRelation::getGoodsCouponId, goodsCoupon.getGoodsCouponId())
//                        .eq(GoodsCouponRelation::getPartsClassifyId,e.getPartsClassifyId()));
//                if(one==null){
//                    GoodsCouponRelation CouponRelation = new GoodsCouponRelation();
//                    CouponRelation.setPartsClassifyId(e.getPartsClassifyId());
////                CouponRelation.setCarPartsShopId(e.getCarPartsShopId());
//                    CouponRelation.setGoodsCouponId(goodsCoupon.getGoodsCouponId());
//                    goodsCouponRelationMapper.insert(CouponRelation);
//                }
//
//            }
//        });

        return 1;
    }

    /**
     * 修改商品优惠券
     *
     * @param goodsCoupon 商品优惠券
     * @return 结果
     */
    @Override
    public int updateGoodsCoupon(GoodsCoupon goodsCoupon)
    {
        goodsCoupon.setUpdateTime(DateUtils.getNowDate());
        return goodsCouponMapper.updateGoodsCoupon(goodsCoupon);
    }

    /**
     * 批量删除商品优惠券
     *
     * @param goodsCouponIds 需要删除的商品优惠券主键
     * @return 结果
     */
    @Override
    public int deleteGoodsCouponByGoodsCouponIds(Long[] goodsCouponIds)
    {
        return goodsCouponMapper.deleteGoodsCouponByGoodsCouponIds(goodsCouponIds);
    }

    /**
     * 删除商品优惠券信息
     *
     * @param goodsCouponId 商品优惠券主键
     * @return 结果
     */
    @Override
    public int deleteGoodsCouponByGoodsCouponId(Long goodsCouponId)
    {
        return goodsCouponMapper.deleteGoodsCouponByGoodsCouponId(goodsCouponId);
    }

    @Override
    public ApiResult sendGoodsCouponByGoodsUserIds(SendGoodsCouponParam sendGoodsCouponParam) {
        GoodsCouponHistory goodsCouponHistory = new GoodsCouponHistory();
        if (sendGoodsCouponParam.getUserIds() != null) {
            List<Long> userIds = sendGoodsCouponParam.getUserIds();
            for (Long userId : userIds) {
                goodsCouponHistory.setGoodsCouponId(sendGoodsCouponParam.getGoodsCouponId());
                goodsCouponHistory.setUserId(userId);
                goodsCouponHistory.setUseStatus("0");
                goodsCouponHistoryMapper.insertGoodsCouponHistory(goodsCouponHistory);
            }
            return ApiResult.success();
        } else {
            return ApiResult.error("请传入正确参数");
        }
    }

    /**
     * 用户查询
     * @param user
     * @return
     */
    @Override
    public ApiResult userList(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();

        return ApiResult.success(userMapper.selectList(new LambdaQueryWrapper<User>()
                .like(User::getNickName,user.getNickName())
                .like(User::getPhoneNumber,user.getPhoneNumber())
                .eq(User::getDelFlag,0)));
    }

    @Override
    public ApiResult goodsList(GooGoods Goods) {

        return ApiResult.success(gooGoodsMapper.selectList(new LambdaQueryWrapper<GooGoods>()
                .eq(GooGoods::getDelFlag,0)));
    }

    @Override
    public void updateCouponPastDue() {
        List<GoodsCoupon> list = goodsCouponMapper.selectList(new LambdaQueryWrapper<GoodsCoupon>()
                .eq(GoodsCoupon::getIsOverdue, 0)
                .eq(GoodsCoupon::getDelFlag, 0));
        list.forEach(coupon->{
            // 结束时间不为空的
            if (coupon.getEndTime()!=null){
                // 当前时间
                LocalDateTime now = LocalDateTime.now();
                // 将时间转为统一格式然后进行比较
                LocalDateTime assignTime = TimeUtil.dateToLocalDateTime(coupon.getEndTime(), "yyyy-MM-dd HH:mm:ss");
                if(assignTime.isBefore(now)){
                    coupon.setIsOverdue("1");
                    this.updateById(coupon);
                }
            }
        });
    }

    @Override
    public SysUserVo2 getUser(Long userId) {
        SysUserVo2 sysUserVo2= gooGoodsMapper.getUser(userId);
        return sysUserVo2;
    }
}
